package cn.sdormitory.knowledgebase.service.impl;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.knowledgebase.dao.*;
import cn.sdormitory.knowledgebase.pojo.*;
import cn.sdormitory.knowledgebase.service.StudySkillsService;
import cn.sdormitory.knowledgebase.vo.RelatedUserVo;
import cn.sdormitory.knowledgebase.vo.SkillBatchVO;
import cn.sdormitory.knowledgebase.vo.StudySkillsVo;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class StudySkillsServiceImpl extends ServiceImpl<StudySkillsDao, StudySkills> implements StudySkillsService {
    @Resource
    private StudySkillsDao studySkillsDao;
    @Resource
    private StudySkillsQuestionsDao studySkillsQuestionsDao;
    @Resource
    private StudySkillsBatchDao studySkillsBatchDao;
    @Resource
    private StudySkillsBatchUserDao studySkillsBatchUserDao;


    @Override
    public CommonPage<StudySkillsVo> getStudySkillsList(String title, Integer type, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudySkillsVo> studySkillsList = studySkillsDao.getStudySkillsList(title, type);
        return CommonPage.restPage(studySkillsList);
    }

    @Override
    public void delSkills(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        studySkillsDao.deleteBatchIds(idList);
    }

    @Override
    public List<SkillBatchVO> getSkillsBatchList(Integer skillId) {
        return studySkillsDao.getSkillsBatchList(skillId);
    }


    @Override
    public StudySkills getStudySkillsById(Integer id) {
        return studySkillsDao.selectById(id);
    }

    @Override
    @Transactional
    public void editSkills(StudySkills studySkills) {
        if(studySkills.getId()!=null) {
            studySkillsDao.updateById(studySkills);
        }else {
            studySkillsDao.insert(studySkills);
            if(!studySkills.getCycle()) { //不是周期学习，则插入一条永久的批次任务
                StudySkillsBatch batch = new StudySkillsBatch();
                batch.setStudySkillsId(studySkills.getId());
                batch.setStudyBatch(0);
                batch.setStatus(false);
                studySkillsBatchDao.insert(batch);
            }
        }
    }

    @Override
    public CommonPage<StudyQuestions> getRelatedQuestionsList(Integer skillsId,Integer category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudyQuestions> list = studySkillsDao.getRelatedQuestionsList(skillsId,category);
        return CommonPage.restPage(list);
    }

    @Override
    public CommonPage<StudyQuestions> getUnRelatedQuestionsList(Integer skillsId,Integer category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudyQuestions> list = studySkillsDao.getUnRelatedQuestionsList(skillsId,category);
        return CommonPage.restPage(list);
    }

    @Override
    @Transactional
    public void relatedQuestions(Integer skillId, Integer[] questionIds) {
        for (Integer questionId : questionIds) {
            StudySkillsQuestions skillsQuestions = new StudySkillsQuestions();
            skillsQuestions.setStudySkillsId(skillId);
            skillsQuestions.setStudyQuestionsId(questionId);
            studySkillsQuestionsDao.insert(skillsQuestions);
        }
    }

    @Override
    public void unRelatedQuestions(Integer skillId, Integer[] questionIds) {
        LambdaQueryWrapper<StudySkillsQuestions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudySkillsQuestions::getStudySkillsId,skillId);

        boolean isFirst = true;
        for (Integer questionId : questionIds) {
            if (isFirst) {
                wrapper.eq(StudySkillsQuestions::getStudyQuestionsId, questionId);
                isFirst = false;
            } else {
                wrapper.or().eq(StudySkillsQuestions::getStudyQuestionsId, questionId);
            }
        }
        studySkillsQuestionsDao.delete(wrapper);
    }

    @Override
    public List<RelatedUserVo> getRelatedUserList(Integer batchId, String userName, Integer deptId) {
        return studySkillsDao.getRelatedUserList(batchId,userName,deptId);
    }

    @Override
    public List<RelatedUserVo> getUnRelatedUserList(Integer batchId, String userName, Integer deptId) {
        return studySkillsDao.getUnRelatedUserList(batchId,userName,deptId);
    }

    @Override
    @Transactional
    public void relatedUsers(Integer batchId, Integer[] userIds) {
        for (Integer userId : userIds) {
            StudySkillsBatchUser batchUser = new StudySkillsBatchUser();
            batchUser.setSkillsBatchId(batchId);
            batchUser.setUserId(userId);
            studySkillsBatchUserDao.insert(batchUser);
        }
    }

    @Override
    public void unRelatedUsers(Integer batchId, Integer[] userIds) {
        LambdaQueryWrapper<StudySkillsBatchUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudySkillsBatchUser::getSkillsBatchId,batchId);

        boolean isFirst = true;
        for (Integer userId : userIds) {
            if (isFirst) {
                wrapper.eq(StudySkillsBatchUser::getUserId, userId);
                isFirst = false;
            } else {
                wrapper.or().eq(StudySkillsBatchUser::getUserId, userId);
            }
        }
        studySkillsBatchUserDao.delete(wrapper);
    }

    @Override
    public StudySkillsBatch getLastBatchBySkillsId(Integer skillId) {
        LambdaQueryWrapper<StudySkillsBatch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudySkillsBatch::getStudySkillsId,skillId)
                .orderByDesc(StudySkillsBatch::getStudyBatch)
                .last("LIMIT 1");
        return studySkillsBatchDao.selectOne(wrapper);
    }

    @Override
    public StudySkillsBatch getBatchById(Integer batchId) {
        return studySkillsBatchDao.selectById(batchId);
    }

    @Override
    public void editBatch(StudySkillsBatch batch) {
        if(batch.getId()!=null) {
            studySkillsBatchDao.updateById(batch);
        }else {
            studySkillsBatchDao.insert(batch);
        }
    }

    @Override
    public void delBatch(Integer id) {
        studySkillsBatchDao.deleteById(id);
    }

    @Override
    @Transactional
    public void openBatch(Integer batchId, Integer skillsId) {
        LambdaUpdateWrapper<StudySkillsBatch> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudySkillsBatch::getStudySkillsId,skillsId);
        updateWrapper.eq(StudySkillsBatch::getStatus,true);
        updateWrapper.set(StudySkillsBatch::getStatus,false);
        studySkillsBatchDao.update(null,updateWrapper);

        StudySkillsBatch batch = new StudySkillsBatch();
        batch.setId(batchId);
        batch.setStatus(true);
        studySkillsBatchDao.updateById(batch);
    }

    @Override
    public void closeBatch(Integer batchId) {
        StudySkillsBatch batch = new StudySkillsBatch();
        batch.setId(batchId);
        batch.setStatus(false);
        studySkillsBatchDao.updateById(batch);
    }

}
